<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------
 *
 * manage menu
 *
 * @package     controllers
 * @subpackage  
 * @category    menu - 菜单管理
 * @author      ChenFu
 * @since       2015-6-11 version 1.0       
 * -----------------------------------------------------    
 */

class Menu extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	  	$this->load->library('form_validation'); // 表单验证类
	  	$this->load->model('menu_model');        // 加载菜单模型
	  	check_login(); // 检查是否登录
	}

	/**
	 * [index 菜单列表]
	 * @return [type] [description]
	 */
	public function index()
	{
		$data['menu'] = $this->menu_model->to_list(0,0,true); // 菜单数据
		
		$this->load->view('admin/menu/index',$data);
	}

	/**
	 * [create 增加菜单]
	 * @return [type] [description]
	 */
	public function create(){
		$data['menu'] = $this->menu_model->to_options(0,0);
		if ($this->form_validation->run('menu') == FALSE){
		   $this->load->view('admin/menu/create',$data);
		}else{
			if(!empty($_POST['menu_name'])){
				if($this->menu_model->insert($_POST)){
					$this->sys_message->skip('增加菜单成功!',site_url('manage/menu'));
				}else{
					$this->sys_message->show_error('增加菜单失败!');
				}
			}
		}
	}

	/**
	 * 更新菜单信息
	 */
	public function  update(){
		$data['arr']  = $this->menu_model->get_menu_by_menu_id($_GET['mid']);
		$data['menu'] = $this->menu_model->to_options(0,0,$data['arr']['menu_pid']);
		if($this->form_validation->run('menu') == FALSE){
			$this->load->view('admin/menu/update',$data);
		}else{
			if(!empty($_POST['menu_name'])){
				if($this->menu_model->update($_POST,$_GET['mid'])){
					$this->sys_message->skip('更新菜单成功!',site_url('manage/menu'));
				}else{
					$this->sys_message->show_error('更新菜单失败!');
				}
			}
		}
	}

	/**
	 * [delete 删除菜单]
	 * @return [type] [description]
	 */
	public function delete(){
		if(!empty($_GET['mid']) && intval($_GET['mid'])){
			$menu = $this->menu_model->get_pid_date($_GET['mid']);
			if(count($menu) != 0){
				die('error_1');
			}
			if($this->menu_model->delete($_GET['mid'])){
				die('ok');
			}else{
				die('error_2');
			}
		}else{
			die('error_3');
		}
	}
}
